package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        // 构造购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 复制属性
        BeanUtils.copyProperties(dto, shoppingCart);
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 根据userId+dishId+dishFlavor查询购物车
        ShoppingCart cart =shoppingCartMapper.selectBy(shoppingCart);
        if (cart == null) {
            // 购物车没有该对象，添加购物车对象到购物车
            if (dto.getDishId() != null) {
                // 添加对象为菜品
                Dish dish = dishMapper.getById(Math.toIntExact(dto.getDishId()));
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 添加对象为套餐
                Setmeal setmeal = setmealMapper.getById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        } else {
            // 购物车有该菜品，数量加1
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumber(cart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(userId);
    }

    /**
     * 减少购物车中的菜品数量
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        // 1)获取用户Id：
        Long userId = BaseContext.getCurrentId();

        // 2)查询删除购物车对象；
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        if (dto.getDishId() != null) cart.setDishId(dto.getDishId());
        if (dto.getSetmealId() != null) cart.setSetmealId(dto.getSetmealId());
        cart.setDishFlavor(dto.getDishFlavor());
        ShoppingCart shoppingCart = shoppingCartMapper.selectByCart(cart);

        if (shoppingCart != null) {
            if (shoppingCart.getNumber() == 1) {
                // 3)如果购物车对象的数量为1，则删除该对象；
                shoppingCartMapper.deleteById(shoppingCart.getId());
            } else {
                // 4)如果购物车对象的数量大于1，则减1，更新该对象
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumber(shoppingCart);
            }
        }
    }
}
